/*
 * @Author: Wangtao
 * @Date: 2020-11-03 20:41:53
 * @LastEditors: zhanwang
 * @LastEditTime: 2022-01-04 11:18:49
 */
/**
 * Created by liyl on 24/09/2020.
 */
import { VuexModule, Module, Mutation, getModule, Action } from 'vuex-module-decorators'
import store from '@/store'
import { isNumber2 } from '@/utils/validate'
import { Message } from 'element-ui'
import { commonAction, checkHideTel } from '@/utils/common'
import { CommonModule } from '@/store/modules/common'
export interface CtiState {
    isCalling: boolean
    superUniqueId: string
    currentStatusObj: any
    updateQueueAgentObj: any
    riskData: any,
    autoCallTaskData: any
}
interface DialoutData {
  calleeNumber: string,
  taskId?: string,
  previewNumberId?: string,
  extraNumber?: string
}

@Module({ dynamic: true, name: 'cti', store, namespaced: false })
class Cti extends VuexModule implements CtiState {
  public isCalling: boolean = false // 当前是否通话中，(呼叫中 || 振铃开始这个值就是true. handup 置为false。)
  public superUniqueId: string = '' // 当前通话中的uniqueId (handUp不清空，如果要判断通话中的id,可以结合isCalling做判断)
  public currentStatusObj: object = { statusNumber: '8', statusName: '未连接', statusTime: 0, callEvent: '' } // 当前座席的状态的对象，用于控制电话条空闲与否
  public updateQueueAgentObj: any = [] // 更新转移，咨询上的技能组内座席状态
  public riskData: any = {} // 风控数据
  public autoCallTaskData: any = {} // 自动外呼弹屏数据

  @Mutation
  public setCurrentEvent (evtJson: any) { // 设置当前状态对象
    const newEvtJson = evtJson
    if (newEvtJson.number) {
      newEvtJson.hideTel = checkHideTel(newEvtJson.number) // 号码隐藏
    }
    this.currentStatusObj = newEvtJson
    this.superUniqueId = evtJson.superUniqueId
    if (evtJson.callType === 'autoDialout' && evtJson.superUniqueId !== this.autoCallTaskData.superUniqueId) {
      const options = {
        dir: 'auto',
        icon: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=405069346,3880007726&fm=11&gp=0.jpg',
        body: `自动外呼任务：${evtJson.taskName}, 客户已接听，点击进行信息填写`
      }
      CommonModule.NOTIFY({
        title: '自动外呼弹屏通知',
        message: '',
        options,
        routePath: `/call_task/index/my_auto_call_task_list?taskId=${evtJson.taskId}`,
        notifyData: {}
      })
      this.autoCallTaskData = evtJson
    }
    if (evtJson.callEvent === 'calling' || evtJson.callEvent === 'ring') { // 外呼： 呼叫中 || 呼入： 振铃
      this.isCalling = true
    } else if (evtJson.callEvent === 'hangup') {
      this.isCalling = false
    }
  }

  @Mutation
  public updateQueueAgent (event: any) { // 设置转移&&咨询上技能组的状态
    this.updateQueueAgentObj = event
  }

  @Mutation
  public setRiskData (data: any) {
    this.riskData = data
  }

  @Action
  public async CtiCommonAction (parame: object) {
    return commonAction(parame).then(resp => {
      return resp
    })
  }

  @Action
  public async CtiDialoutAction (data: DialoutData) {
    const value = window.sessionStorage.getItem('sessionForm')
    let localForm = { loginType: 'WEBRTC' }
    if (value && value !== 'undefined' && value !== 'null') {
      localForm = JSON.parse(value)
    }
    if (!isNumber2(data.calleeNumber) || !data.calleeNumber) { // 不是纯数字 || 没有手机号
      Message.error('请输入正确的号码')
      return new Promise((resolve) => { // 直接返回false
        resolve(false)
      })
    }
    // const routePath = `/call_workBench/index/cdr_outbound/${data.superUniqueId}?phone=${data.calleeNumber}`
    const actionData = { ...{ loginType: localForm.loginType }, ...data }
    return commonAction({ url: '/call/action/biz/dialOut', method: 'POST', data: actionData }).then(resp => {
      if (!resp.success && (resp.code === '6727' || resp.code === '6728') && resp.data) {
        const riskData = resp.data
        riskData.riskDialog = true
        riskData.code = resp.code
        riskData.phoneNum = data.calleeNumber
        this.setRiskData(riskData)
      } else if (!resp.success) {
        Message.error(resp.message)
      }
      return resp
    }).catch(() => {
      return new Promise((resolve) => { // 直接返回false
        resolve(false)
      })
    })
  }
}

export const CtiModule = getModule(Cti)
